#!/bin/dash

cd

if [ ! -d ~/.emacs.d ]; then
    git clone --depth=1 https://codeberg.org/srv/.emacs.d ~/.emacs.d
    ( cd ~/.emacs.d && make pull )
fi

if [ -d ~/.emacs.d ] && [ ! -d ~/.emacs.d/.git ]; then
 TMP=$(echo -n $(mktemp -u | cut -d. -f2))
 mv ~/.emacs.d "$HOME/.emacs.d.$TMP.backup"
 git clone --depth=1 https://codeberg.org/srv/.emacs.d ~/.emacs.d
 ( cd ~/.emacs.d && make pull )
fi

#if false; then
# wget https://c.hgit.ga/software/hyperterm.git/plain/install.sh -O ~/.install.sh
# sh ~/.install.sh
#fi

if [ -d ~/.emacs.d/.git ]; then
 if lsb_release -a 2>&1 | grep -q yperbola; then
		doas pacman -S --noconfirm xdg-user-dirs
 fi

 if [ -d ~/.emacs.d/hyperterm ]; then
		ln -svf ~/.emacs.d/hyperterm ~/.hyperterm
 fi

 if [ -f ~/.bashrc ]; then
		mv ~/.bashrc ~/.bashrc.bk
		ln -svf ~/.emacs.d/p/ln/bashrc ~/.bashrc
 fi

 if [ -f ~/.hyperterm/_custom.sh ] && \
		 [ -f ~/.emacs.d/p/scripts.sh ] && \
		 ! grep -q '.emacs.d/p/s/bash' ~/.hyperterm/_custom.sh; then
		cat ~/.emacs.d/p/scripts.sh >> ~/.hyperterm/_custom.sh
 fi

 if [ -f ~/.emacs.d/p/simbolic-links-installer.sh ]; then
		sh ~/.emacs.d/p/simbolic-links-installer.sh
 fi

 if false; then
		if [ ! -d ~/.hyperbola-dbus-recover ]; then
		 git clone --depth=1 https://notabug.org/saravia/hyperbola-dbus-recover.git ~/.hyperbola-dbus-recover

		 if [ -d ~/.hyperbola-dbus-recover ]; then
				cd
				doas chmod 755 /usr/sbin
				doas chmod 755 /sbin
				doas pacman -U --noconfirm ~/.hyperbola-dbus-recover/pambase.lz
				doas pacman -U --noconfirm ~/.hyperbola-dbus-recover/pam.lz
				doas pacman -U --noconfirm ~/.hyperbola-dbus-recover/libelogind.lz
				doas pacman -U --noconfirm ~/.hyperbola-dbus-recover/elogind.lz --assume-installed dbus
				doas pacman -U --noconfirm ~/.hyperbola-dbus-recover/dbus.lz
				doas pacman -U --noconfirm ~/.hyperbola-dbus-recover/libdbusmenu-glib.lz ~/.hyperbola-dbus-recover/libdbusmenu-gtk2.lz ~/.hyperbola-dbus-recover/libdbusmenu-gtk3.lz
				doas useradd --system --no-create-home dbus
				doas cp -v ~/.hyperbola-dbus-recover/usr/lib/* /usr/lib/
				doas ln -svf /usr/lib/libatspi.so.0.0.1 /usr/lib/libatspi.so.0
				doas ln -svf /usr/lib/libdbus-c++-1.so.0.0.0 /usr/lib/libdbus-c++-1.so.0
				doas ln -svf /usr/lib/libdbus-c++-glib-1.so.0.0.0 /usr/lib/libdbus-c++-glib-1.so.0
				doas ln -svf /usr/lib/libdbus-glib-1.so.2.3.3 /usr/lib/libdbus-glib-1.so.2
				doas cp -v ~/.hyperbola-dbus-recover/usr/lib/x86_64-linux-gnu/libatk-bridge-2.0.so.0.0.0 /usr/lib64/
				doas ln -svf /usr/lib64/libatk-bridge-2.0.so.0.0.0 /usr/lib64/libatk-bridge-2.0.so.0
		 fi
		fi

		if [ ! -f /opt/bin1/chrome ] && [ -d ~/.hyperbola-dbus-recover ]; then
		 mkdir -p ~/.bin1
		 cd ~/.bin1
		 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
		 ar x google-chrome-stable_current_amd64.deb
		 tar -xf data.tar.xz
		 doas ln -svf ~/.bin1/opt/google/chrome /opt/bin1
		fi
 fi

 if [ -d ~/.emacs.d ]; then
		emacs --batch --eval='(load-file "~/.emacs.d/init.el")'
		sh ~/.emacs.d/p/simbolic-links-installer.sh
 fi

 wget https://notabug.org/srv/s/raw/master/mario.smc -O ~/.mario.smc

 mkdir -p ~/Descargas

 #if false; then
 # wget https://nodejs.org/dist/v18.12.1/node-v18.12.1-linux-x64.tar.gz -O ~/Descargas/node-v18.12.1-linux-x64.tar.gz
 # gunzip -c ~/Descargas/node-v18.12.1-linux-x64.tar.gz > ~/Descargas/node-v18.12.1-linux-x64
 # tar --get -f ~/Descargas/node-v18.12.1-linux-x64
 # doas ln -svf ~/Descargas/node-v18.12.1-linux-x64/bin /opt/bin2 && echo 'NODE STABLE INSTALADO' || echo 'NODE NO INSTALADO'
 #fi

 if cat /etc/makepkg.conf | grep -qv 'Sr. Saravia <saravia@riseup.net>'; then
		doas sed -e 's|#PACKAGER="John Doe <john@doe.com>"|PACKAGER="Sr. Saravia <saravia@riseup.net>"|' -i /etc/makepkg.conf
		doas sed -e 's|#GPGKEY=""|GPGKEY="65D904D101D79A26CCF9B45A5513088F998E8FF8"|' -i /etc/makepkg.conf
 fi

 doas chmod 600 ~/.ssh/id_ed25519

 if false; then
		cp -rf /etc/xdg/echinus ~/.echinus
		sed -i 's|A|W|g' ~/.echinus/echinusrc
		sed -i 's|W + j|WS + j|g' ~/.echinus/echinusrc
		sed -i 's|W + k|WS + k|g' ~/.echinus/echinusrc
 fi

 if false; then
		wget https://nixos.org/nix/install -O ~/.nixos-install.sh
		echo ACEPTA
		doas sh ~/.nixos-install.sh --daemon
 fi

 if false; then
		echo 'cd && git clone https://notabug.org/srv/hyperbola-upd-gcc && sleep 0.5 && cd hyperbola-upd-gcc && sh 0001-bash-Instalar-mis-llaves.sh && sleep 0.5 && sh 0002-bash-Descargar-fuentes-y-compilar.sh' > ~/compile.sh
		echo '#!/bin/sh' > ~/.xinitrc
		echo "exec echinus & emacs --eval='(find-file \"~/compile.sh\")'" >> ~/.xinitrc
 else
     if false; then
		echo '#!/bin/dash' > ~/.xinitrc
		echo "touch ~/.is-exwm-enabled && exec emacs --eval='(load-file \"~/.emacs.d/init-exwm.el\")' & setxkbmap -layout latam" >> ~/.xinitrc
     fi
     XINITRC=~/.emacs.d/p/ln/.xinitrc;ln -svf $XINITRC  ~/
 fi

 startx
fi
